package com.usian.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.usian.dao.ConsigneeDao;
import com.usian.dao.RegionDao;
import com.usian.pojo.*;
import com.usian.service.ConsigneeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class ConsigneeServiceImpl implements ConsigneeService {

    @Autowired
    private ConsigneeDao consignsDao;
    @Autowired
    private RegionDao regionDao;

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage() == null ? 1 : queryPageBean.getCurrentPage(), queryPageBean.getPageSize() == null ? 10 : queryPageBean.getPageSize());
        List<Consignee> consigneeList = consignsDao.findPage(queryPageBean);
        PageInfo<Consignee> pageInfo = new PageInfo<>(consigneeList);
        PageResult pageResult = new PageResult();
        pageResult.setRows(pageInfo.getList());
        pageResult.setTotal(pageInfo.getTotal());
        return pageResult;
    }

    @Override
    public void deleteById(Integer id) {
        consignsDao.deleteById(id);
    }

    @Override
    public List<Region> findPCC(Integer id) {
        LambdaQueryWrapper<Region> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Region::getPid,id);
        return regionDao.selectList(wrapper);
    }

    @Override
    public Consignee findById(Integer id) {
        return consignsDao.findById(id);
    }

    @Override
    public void addConsignee(Consignee consignee) {
        if (consignee!=null){
            if (consignee.getCounty()!=null){
                consignee.setCountyId(consignee.getCounty());
            }else {
                consignee.setCountyId(consignee.getCity());
                if (consignee.getCity()==null){
                    consignee.setCountyId(consignee.getProvince());
                }
            }
        }
        consignsDao.insert(consignee);
    }

    @Override
    public void deleteConsignee(String ids) {
        String[] idSr = ids.split(",");
        consignsDao.deleteForEach(idSr);
//        if (idSr!=null&&idSr.length>0){
//            for (String id : idSr) {
//                consignsDao.deleteById(id);
//            }
//        }
    }

    @Override
    public void upConsignee(Consignee consignee) {
        consignee.setCountyId(consignee.getCounty());
        consignsDao.updateById(consignee);
    }

}
